import Vue from 'vue'
import Router from 'vue-router'
/**
 * 导入组件
 */
import Login from '@/components/views/Login'
import View from '@/View'

// 引入类目JS文件
import Category from './modules/category'

Vue.use(Router)

let routerList = [
  {
    path: '/',
    name: 'Login',
    component: Login,
    meta: {
      title: '慧慧返利管理系统后台 - 登录页面'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '慧慧返利管理系统后台 - 登录页面'
    }
  },
  {
    path: '/main',
    name: 'main',
    component: View,
    children: [
      {
        path: 'index',
        component: resolve => require(['@/components/views/main/Index'], resolve),
        meta: {
          title: '慧慧返利管理系统后台 - 首页',
          name: '首页'
        }
      }
    ]
  },
  Category
]

/**
 * 注册路由并设置为经典模式
 * @type {VueRouter} 类型
 */
const router = new Router({
  mode: 'history',
  routes: routerList
})

/**
 * 读取设置标题
 */
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

const routerPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return routerPush.call(this, location).catch(error => error)
}

export default router
